<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<style>
    td{
        text-align:center
    }
</style>
<body>
    <div id="app" style="margin-top: 200px;">
        <table border="1px" width="50%" cellspacing="0" align="center">
            <tr>
                <th width="50px"></th>
                <th>书籍名称</th>
                <th>出版日期</th>
                <th>价格</th>
                <th>购买数量</th>
                <th>操作</th>
            </tr>

            <tr v-for="(book,index) in bookArr">
                <td>{{index + 1}}</td>
                <td>{{book.bookname}}</td>
                <td>{{book.date}}</td>
                <td>￥{{book.price}}</td>
                <td><input type="button" value="-" @click="subNum(index)">{{book.count}}<input type="button" value="+" @click="addNum(index)"></td>
                <td><input type="button" value="移除" @click="del(index)"></td>
            </tr>
        </table>
        <h2 style="margin-left: 370px;">总价格:￥{{sumPrice.toFixed(2)}}</h2>
        
    </div>
</body>
<script>
    new Vue({
        el : "#app",
        data : {
            bookArr : [{"bookname":"《java编程思想》","date":"2020-09","price":"98.00","count":1},
            {"bookname":"《数据分析与数据原理》","date":"2019-02","price":"39.00","count":1},
            {"bookname":"《Hadoop权威指南》","date":"2020-10","price":"59.00","count":1},
            {"bookname":"《代码大全》","date":"2018-08","price":"128.00","count":1}],
           
        },
        computed: {
            sumPrice : function(){
                var sumPrice = 0;
                for(var i=0;i< this.bookArr.length;i++){
                    sumPrice += (parseFloat(this.bookArr[i].price) * parseInt(this.bookArr[i].count));
                }
                return sumPrice;
            }
        },
        methods : {
            subNum(index){
               if(this.bookArr[index].count > 1){
                    this.bookArr[index].count--;
               }
            },

            addNum(index){
               this.bookArr[index].count++;
            },
            del(index){
                this.bookArr.splice(index,1);
            }

        }
    })

</script>
</html>